package com.ease.framework.gef4p.figures.impl {
   import com.ease.util.CursorManagerExt;
   import com.ease.framework.figures.IFigure;
   import com.ease.framework.gef4p.figures.IDraggingFigure;
   import com.ease.framework.gef4p.figures.ILabelFigure;
   import com.ease.framework.gef4p.figures.ILabelFigureBox;
   import com.ease.framework.gef4p.figures.IVertexFigure;
   
   import flash.events.MouseEvent;
   import flash.geom.Point;

   public class VertexFigure extends FigureWithHandles implements IVertexFigure, ILabelFigureBox {
      protected var labelFigure:ILabelFigure;
      private var _allowDrag:Boolean = true;

      public function VertexFigure() {
         super();
         this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
      }

      public function onMouseOut(event:MouseEvent):void {
         if (!event.buttonDown) {
            if (CursorManagerExt.currentHandleCursorID > 0)
               CursorManagerExt.removeCursor(CursorManagerExt.currentHandleCursorID);
         }
      }

      override protected function createChildren():void {
         super.createChildren();

      }

      public function findFigureAt(stageX:Number, stageY:Number):IFigure {
         var i:int;
         //从最上层图形开始遍历
         for (i = this.numChildren - 1; i >= 0; i--) {
            if (getChildAt(i) is IFigure) {
               var childFigure:IFigure = getChildAt(i) as IFigure;
               if (childFigure.hitTestPoint(stageX, stageY)) {
                  return childFigure.findFigureAt(stageX, stageY);
               }
            }
         }
         return this;
      }

      public function getLabelFigure():ILabelFigure {
         if (labelFigure == null) {
            setLabelFigure(new LabelFigure());
         }
         return labelFigure;
      }

      public function setLabelFigure(figure:ILabelFigure):void {
         labelFigure = figure;
         figure.setContainer(this);
      }

      public function onMoving():void {

      }

      public function getDraggingFigure():IDraggingFigure {
         return new VertexDraggingFigure(this);
      }

      public function refreshFigureWithSubstitute(sfigure:IDraggingFigure):void {
         var point:Point = sfigure.parent.localToGlobal(new Point(sfigure.x, sfigure.y));
         point = parent.globalToLocal(point);
         x = point.x;
         y = point.y;
         width = sfigure.width;
         height = sfigure.height;
         validateNow();
      }

      public function get draggingWidth():Number {
         return width;
      }

      public function get draggingHeight():Number {
         return height;
      }
      
      public function get allowDrag():Boolean {
         return _allowDrag;
      }
      
      public function set allowDrag(value:Boolean):void {
         _allowDrag = value;
      }

      public function set draggingWidth(value:Number):void {
         width = value;
      }

      public function set draggingHeight(value:Number):void {
         height = value;
      }

   }
}
import mx.core.UIComponent;
import com.ease.framework.gef4p.figures.IDraggingFigure;
import com.ease.framework.gef4p.figures.IVertexFigure;
import flash.geom.Point;

class VertexDraggingFigure extends UIComponent implements IDraggingFigure {

   private var originalFigure:IVertexFigure;

   public function VertexDraggingFigure(of:IVertexFigure) {
      originalFigure = of;
      //默认为false。当拖动开始的时候才更改为true.
      this.visible = false;
   }

   override protected function createChildren():void {
      var point:Point = originalFigure.parent.localToGlobal(new Point(originalFigure.x, originalFigure.y));
      point = parent.globalToLocal(point);
      x = point.x;
      y = point.y;
      width = originalFigure.draggingWidth;
      height = originalFigure.draggingHeight;
      minWidth = originalFigure.minWidth;
      minHeight = originalFigure.minHeight;
   }

   public function getOriginalFigure():IVertexFigure {
      return originalFigure;
   }

   override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
      super.updateDisplayList(unscaledWidth, unscaledHeight);
      graphics.clear();
      paint();
   }

   protected function paint():void {
      graphics.lineStyle(1, 0x8888AA); //灰蓝色
      graphics.beginFill(0xB3D0C2, 0.4);
      graphics.drawRect(0, 0, this.width, this.height);
   }
}